package com.xinyu.controller;


import com.xinyu.config.FileStorageProperties;
import com.xinyu.domain.Public;
import com.xinyu.service.IPublicService;
import com.xinyu.utils.JWTUtils;
import com.xinyu.vo.BaseResult;
import com.xinyu.vo.PublicRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author: XinYu
 * @since 2025-02-16
 */
@RestController
@RequestMapping("/public")
public class PublicController {
    @Autowired
    private IPublicService publicService;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private FileStorageProperties fileStorageProperties;

    @PostMapping("/queryByCondition")
    public ResponseEntity<BaseResult> queryByCondition(@RequestBody PublicRequest publicRequest){
        //publicRequest.setLoginUserId((Integer) SecurityContextHolder.getContext().getAuthentication().getDetails());
        BaseResult br = publicService.queryByCondition(publicRequest);
        return ResponseEntity.ok(br);
    }

    @PostMapping("/update")
    public ResponseEntity<BaseResult> modify(@RequestBody Public pub){
        if (StringUtils.isNotBlank(pub.getAttachment()))
            pub.setAttachment(pub.getAttachment().replace(fileStorageProperties.getBaseUrl(),""));
        publicService.updateById(pub);
        return ResponseEntity.ok(BaseResult.ok());
    }

    @PostMapping("/save")
    public ResponseEntity<BaseResult> add(@RequestBody Public pub){
        // 1 当前登录用户的id
        String token = request.getHeader("Authorization");
        pub.setUserId(JWTUtils.getUserIdFromToken(token));

        //2 设置发布时间-当前系统时间
        LocalDateTime now = LocalDateTime.now();
        // 格式化时间（可选）
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedDateTime = now.format(formatter);
        pub.setPublishTime(formattedDateTime);

        pub.setReadcount(0);

        publicService.save(pub);
        return ResponseEntity.ok(BaseResult.ok());
    }

    @GetMapping("/query/{publicId}")
    public ResponseEntity<BaseResult> queryPublicById(@PathVariable Integer publicId){
        Public pc = publicService.queryPublicById(publicId);
        return ResponseEntity.ok(BaseResult.ok(pc));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<BaseResult> deleteById(@PathVariable Integer id) {
        Public pc = publicService.getById(id);
        pc.setIsDelete(1);

        boolean success = publicService.updateById(pc);
        return ResponseEntity.ok(BaseResult.ok());
    }
}

